<?php

// Copyright 2019 Hackware SpA <human@hackware.cl>
// "Hackware Web Services Core" is released under the MIT License terms.

namespace Hawese\Tests;

use Hawese\Core\TableModelCollection;
use Laravel\Lumen\Testing\DatabaseTransactions;

class TableModelCollectionTest extends TestCase
{
    use DatabaseTransactions;

    public function setUp(): void
    {
        parent::setUp();
        for ($i = 0; $i < 10; $i++) {
            (new DumbTableModel([
                'attr1' => bin2hex(random_bytes(12)),
                'foreign_id' => (new ForeignTableModel())->insert()
            ]))->insert();
        }

        $this->dumbObjs = new TableModelCollection(
            DumbTableModel::select()->get(),
            DumbTableModel::class
        );
    }

    public function testConstructAndGet()
    {
        $this->assertInstanceOf(
            \Illuminate\Support\Collection::class,
            $this->dumbObjs->get()
        );
        $this->assertInstanceOf(
            DumbTableModel::class,
            $this->dumbObjs->get()->last()
        );
        $this->assertInstanceOf(
            DumbTableModel::class,
            $this->dumbObjs->get()->random()
        );
    }

    public function testAppendForeignObjects()
    {
        $this->dumbObjs->appendForeignObjects(['foreign']);

        $this->assertIsInt(
            $this->dumbObjs->get()->random()->foreign->id
        );
        $this->assertInstanceOf(
            ForeignTableModel::class,
            $this->dumbObjs->get()->random()->foreign
        );
    }
}
